package com.znf4.id.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.znf4.config.RedisConfig;
import com.znf4.constants.CommonConst;
import com.znf4.id.IdGenerator;
import com.znf4.id.utils.IdGeneratorUtil;
import com.znf4.utils.DateUtils;

/**
 * 分布式ID生成接口实现类.
 * 
 * @author .
 *
 */
@Component("idGenerator")
public class IdGeneratorImpl implements IdGenerator {
	
	private static final Log LOG = LogFactory.getLog(IdGeneratorImpl.class);

	/** Redis主节点的IP地址（主备模式） */
	private static String masterNodeIp = RedisConfig.ID_GENERATOR_REDIS_MASTER_IP;
	/** Redis主节点的端口 */
	private static int masterNodePort = RedisConfig.ID_GENERATOR_REDIS_MASTER_PORT;
	/** Redis主节点的请求密码 */
	private static String masterNodePassword = RedisConfig.ID_GENERATOR_REDIS_MASTER_PASSWORD;
	/** Redis主节点的Lua脚本Sha码 */
	private static String masterNodeLuaSha = RedisConfig.ID_GENERATOR_REDIS_MASTER_LUASHA;
	
	/** Redis主节点的String 中 key  user ,value */
	private static String masterNodeStringUserKey = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_USER_KEY ;
	private static String masterNodeStringUserValue = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_USER_VALUE;
	
	/** Redis主节点的String 中 key  account ,value*/
	private static String masterNodeStringAccountKey = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_ACCOUNT_KEY;
	private static String masterNodeStringAccountValue = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_ACCOUNT_VALUE;
	
	/** Redis主节点的String 中 key  order ,value*/
	private static String masterNodeStringOrderKey = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_ORDER_KEY;
	private static String masterNodeStringOrderValue = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_ORDER_VALUE;
	
	/** Redis主节点的String 中 key  resource ,value*/
	private static String masterNodeStringResourceKey = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_RESOURCE_KEY;
	private static String masterNodeStringResourceValue = RedisConfig.ID_GENERATOR_REDIS_MASTER_STRING_RESOURCE_VALUE;
	

	/** Redis备用节点的IP地址（主备模式） *//*
	private static String backupNodeIp = RedisConfig.ID_GENERATOR_REDIS_BACKUP_IP;
	*//** Redis备用节点的端口 *//*
	private static int backupNodePort = RedisConfig.ID_GENERATOR_REDIS_BACKUP_PORT;
	*//** Redis备用节点的请求密码 *//*
	private static String backupNodePassword = RedisConfig.ID_GENERATOR_REDIS_BACKUP_PASSWORD;
	*//** Redis备用节点的Lua脚本Sha码 *//*
	private static String backupNodeLuaSha = RedisConfig.ID_GENERATOR_REDIS_BACKUP_LUASHA;*/

	private static IdGeneratorUtil idGenerator = null;
	

	static {
		try {			
			idGenerator = IdGeneratorUtil.builder()
					.addHost(masterNodeIp, masterNodePort, masterNodePassword, masterNodeLuaSha)
					//.addHost(backupNodeIp, backupNodePort, backupNodePassword, backupNodeLuaSha)
					.build();
			
			//创建String 结构为生成有序id 做准备
			if(!idGenerator.existsString(masterNodeStringUserKey)){
				//用户信息
				idGenerator.addString(masterNodeStringUserKey, masterNodeStringUserValue) ;
			}else if(!idGenerator.existsString(masterNodeStringAccountKey)){
				//账户
				idGenerator.addString(masterNodeStringAccountKey, masterNodeStringAccountValue) ;
			}else if(!idGenerator.existsString(masterNodeStringOrderKey)){
				//订单
				idGenerator.addString(masterNodeStringOrderKey, masterNodeStringOrderValue) ;
				
			}else if(!idGenerator.existsString(masterNodeStringResourceKey)){
				//资源
				idGenerator.addString(masterNodeStringResourceKey, masterNodeStringResourceValue) ;
			}
			
			
			
		} catch (Exception e) {
			LOG.error("=== load and init mq exception:" + e);
		}
	}

	@Override
	public String getId(String tab, long shardId) {
		try {
			long id = idGenerator.next(tab, shardId);
			return String.valueOf(id);
		} catch (Exception e) {
			throw e;
		}
	}


	@Override
	public String getNotifyRecordId() {
		return "NTF"+getId("ntf", 123456789);
	}

	@Override
	public String getNotifyRecordLogId() {
		return "NTFLOG"+getId("ntflog", 123456789);
	}


	@Override
	public String getGoodNameNumber() {
		return "GNN"+getId("gnn", 123456789);
	}


	@Override
	public String getAdNameNumber() {
		return "AD"+getId("ad", 123456789);
	}


	@Override
	public String getActiveNameNumber() {
		return "ACTIVE"+getId("active", 123456789);
	}


	@Override
	public String getLookNameNumber() {
		return "LOOK"+getId("look", 123456789);
	}


	@Override
	public String getUserId() {
		
		String result = idGenerator.getIncr(masterNodeStringUserKey) ;
		// 20位的支付流水号规范：'8888' + yyyyMMdd(时间) + 序列的后8位
		String dateString = DateUtils.toString(new Date(), "yyyyMMdd");
		String trxNo = CommonConst.USER_NO_PREFIX + dateString + result ;
		return trxNo;
		
	}

	
	@Override
	public String getPaymentOrderId() {
		
		String result = idGenerator.getIncr(masterNodeStringOrderKey) ;
		// 20位的支付流水号规范：'1111' + yyyyMMdd(时间) + 序列的后8位
		String dateString = DateUtils.toString(new Date(), "yyyyMMdd");
		String trxNo = CommonConst.MERCHANT_ORDER_NO_PREFIX + dateString + result ;
		return trxNo;
		
	}


	@Override
	public String getAccountInAndOutId() {
		
		String result = idGenerator.getIncr(masterNodeStringAccountKey) ;
		// 20位的支付流水号规范：'9999' + yyyyMMdd(时间) + 序列的后8位
		String dateString = DateUtils.toString(new Date(), "yyyyMMdd");
		String trxNo = CommonConst.ACCOUNT_NO_PREFIX + dateString + result ;
		return trxNo;
	}


	@Override
	public String getPlatformRresourceHumanNumber() {
		return "HUMAN-"+getId("human", 123456789);
	}


	@Override
	public String getPlatformRresourceMechanicsNumber() {
		/*String result = idGenerator.getIncr(masterNodeStringAccountKey) ;
		// 20位的支付流水号规范：'5555' + yyyyMMdd(时间) + 序列的后8位
		String dateString = DateUtils.toString(new Date(), "yyyyMMdd");
		String trxNo = "MECHANICS-"+CommonConst.PLATFROM_RESOURCE_NO + dateString + result ;
		return trxNo;*/
		return "MECHANICS-"+getId("mechanics", 123456789);
	}
	
	

}
